package notes.java.java8.demo.streams;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**分组*/
public class S2_Groupby {
	
	public static void main(String[] args) {
		// 测试数据
		List<User> users = new ArrayList<>();
		User user = new User();
		user.setAge(36);
		user.setSex("男");
		user.setName("zhangsan");
		users.add(user);
		
		User user2 = new User();
		user2.setAge(22);
		user2.setSex("男");
		user2.setName("lisi");
		users.add(user2);
		
		User user3 = new User();
		user3.setAge(25);
		user3.setSex("女");
		user3.setName("lili");
		users.add(user3);
		
		// 根据性别分组
		Map<String, List<User>> map = users.stream().collect(Collectors.groupingBy(User::getSex));
		
		// 输出
		for(String key :map.keySet()){
			System.out.println(key + "==========");
			List<User> us = map.get(key);
			us.forEach(u->{
				System.out.println(u.getName() + "\t" + u.getAge() + "\t" + u.getSex());
			});
		}
	}

}

/**
 * @author wangcs
 *
 */
class User {
	private String name;
	private int age;
	private String sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}